package indi.zhifa.study2025.oa.service.role.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.engine.core.common.entity.web.ServiceException;
import indi.zhifa.study2025.oa.dao.gen.service.IGenMenuDbService;
import indi.zhifa.study2025.oa.dao.gen.service.IGenRelRoleDataDbService;
import indi.zhifa.study2025.oa.dao.gen.service.IGenRelRoleMenuDbService;
import indi.zhifa.study2025.oa.dao.gen.service.IGenRoleDbService;
import indi.zhifa.study2025.oa.dao.mybatis.role.mapper.RoleMapper;
import indi.zhifa.study2025.oa.dao.mybatis.role.mapperOut.RoleSimpleMapperOut;
import indi.zhifa.study2025.oa.dao.po.*;
import indi.zhifa.study2025.oa.service.role.IRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class RoleServiceImpl implements IRoleService {

    final RoleMapper roleMapper;
    final IGenRoleDbService mGenRoleDbService;
    final IGenRelRoleMenuDbService mGenRelRoleMenuDbService;
    final IGenRelRoleDataDbService mGenRelRoleDataDbService;
    final IGenMenuDbService mGenMenuDbService;

    @Override
    public GenRoleEntity checkById(Long id) {
        GenRoleEntity roleEntity = mGenRoleDbService.check(id);
        return roleEntity;
    }

    @Override
    public GenRoleEntity create(GenRoleEntity role) {

        String roleCode = role.getCode();
        LambdaUpdateWrapper<GenRoleEntity> queryWrapper = new LambdaUpdateWrapper<GenRoleEntity>()
                .eq(GenRoleEntity::getCode,roleCode);
        if(mGenRoleDbService.exists(queryWrapper)) {
            throw new ServiceException("已存在code为"+roleCode+"的角色");
        }

        if(!mGenRoleDbService.save(role)){
            throw new ServiceException("存储GenRoleEntity失败");
        }
        return role;
    }

    @Override
    public GenRoleEntity edit(Long pId, Object pRoleEditDto) {
        GenRoleEntity roleEntity = mGenRoleDbService.updatePull(pId, pRoleEditDto);
        return roleEntity;
    }

    @Override
    public List<RoleSimpleMapperOut> listByUserId(Long pUserId) {
        List<RoleSimpleMapperOut> roleSimpleMapperOut = roleMapper.listByUserId(pUserId);
        return roleSimpleMapperOut;
    }

    @Override
    public List<GenRoleEntity> findAll() {
        return mGenRoleDbService.list();
    }

    @Override
    public Map<Long, List<String>> getRelDepCodeList(List<Long> pRoleIdList) {

        List<GenRelRoleMenuEntity> roleEntityList;

        if(CollectionUtils.isEmpty(pRoleIdList)) {
            roleEntityList = mGenRelRoleMenuDbService.list();
        }else{
            LambdaUpdateWrapper<GenRelRoleMenuEntity> queryWrapper = new LambdaUpdateWrapper<GenRelRoleMenuEntity>()
                    .in(GenRelRoleMenuEntity::getRoleId,pRoleIdList);
            roleEntityList = mGenRelRoleMenuDbService.list(queryWrapper);
        }
        Map<Long, List<String>> rtn = roleEntityList.stream().collect(
                Collectors.groupingBy(GenRelRoleMenuEntity::getRoleId,
                        Collectors.mapping(GenRelRoleMenuEntity::getMenuCodeCascade,Collectors.toList())));

        return rtn;
    }

    @Override
    public Map<Long, List<String>> getRelAuthDataCodeList(List<Long> pRoleIdList) {

        List<GenRelRoleDataEntity> roleEntityList;
        if(CollectionUtils.isEmpty(pRoleIdList)){
            roleEntityList = mGenRelRoleDataDbService.list();
        }else{
            LambdaUpdateWrapper<GenRelRoleDataEntity> queryWrapper = new LambdaUpdateWrapper<GenRelRoleDataEntity>()
                    .in(GenRelRoleDataEntity::getRoleId,pRoleIdList);
            roleEntityList = mGenRelRoleDataDbService.list(queryWrapper);
        }
        Map<Long, List<String>> rtn = roleEntityList.stream().collect(
                Collectors.groupingBy(GenRelRoleDataEntity::getRoleId,
                        Collectors.mapping(GenRelRoleDataEntity::getDataCode,Collectors.toList())));
        return rtn;
    }

    @Override
    public List<GenRelRoleMenuEntity> setMenus(Long pRoleId, List<Long> pMenuIdList) {

        LambdaQueryWrapper<GenMenuEntity> menuQueryMapper = Wrappers.<GenMenuEntity>lambdaQuery()
                .in(GenMenuEntity::getId,pMenuIdList)
                .select(GenMenuEntity::getId,GenMenuEntity::getCodeCascade);
        List<GenMenuEntity> menuEntityList = mGenMenuDbService.list(menuQueryMapper);
        Map<Long,GenMenuEntity> menuEntityMap = menuEntityList.stream().collect(Collectors.toMap(GenMenuEntity::getId,v->v));
        if(menuEntityList.size() < pMenuIdList.size()){
            List<Long> missIds = pMenuIdList.stream().filter(depId -> !menuEntityMap.containsKey(depId)).collect(Collectors.toList());
            throw new ServiceException("菜单Id"+ StrUtil.join(",", missIds)+"数据丢失");
        }
        GenRoleEntity roleEntity = mGenRoleDbService.checkMin(pRoleId);
        List<GenRelRoleMenuEntity> relList = new ArrayList<>();
        for(GenMenuEntity menuEntity : menuEntityList){
            GenRelRoleMenuEntity relRoleMenuEntity = new GenRelRoleMenuEntity();
            relRoleMenuEntity.createInit();
            relRoleMenuEntity.setRoleId(roleEntity.getId());
            relRoleMenuEntity.setRoleCode(roleEntity.getCode());
            relRoleMenuEntity.setMenuId(menuEntity.getId());
            relRoleMenuEntity.setMenuCodeCascade(menuEntity.getCodeCascade());
            relList.add(relRoleMenuEntity);
        }

        LambdaUpdateWrapper<GenRelRoleMenuEntity> deleteWrapper = new LambdaUpdateWrapper<GenRelRoleMenuEntity>()
                .eq(GenRelRoleMenuEntity::getRoleId,roleEntity.getId());
        mGenRelRoleMenuDbService.remove(deleteWrapper);
        mGenRelRoleMenuDbService.saveBatch(relList);
        return relList;
    }
}
